import numeral from 'numeral';

let pt = {
  delimiters: {
    thousands: '.',
    decimal: ',',
  },
  // abbreviations: {
  //     thousand: 'k',
  //     million: 'm',
  //     billion: 'b',
  //     trillion: 't'
  // },
  // ordinal : function (number) {
  //     return number === 1 ? 'er' : 'ème';
  // },
  // currency: {
  //     symbol: '€'
  // }
};

let localeMapping = {
  'pt-BR': pt,
  'pt': pt,
};
if (window.readCookie) {
  let teld_locale = readCookie('teld_locale');
  let locale = localeMapping[teld_locale];
  if (locale) {
    numeral.register('locale', teld_locale, locale);
    numeral.locale(teld_locale);
  }
}

// export default function execute(expression) {

export default function (value, formatStr) {
  // debugger;
  // if (value === null) return ""
  return value < 0
    ? isNegative(value, formatStr)
    : numeral(value).format(formatStr || '');
}

function isNegative(value, formatStr) {
  let isNegativeCorrection = false;
  if (value < 0) {
    isNegativeCorrection = true;
    value = value * -1;
  }

  let returnValue = numeral(value).format(formatStr || '');

  if (isNegativeCorrection) {
    returnValue = `-${returnValue}`;
  }
  return returnValue;
}
